package com.arrogames.blondbattle.game.purchases 
{
	import ash.tools.ListIteratingSystem;
	import com.arrogames.blondbattle.game.purchases.Cooldown;
	import com.arrogames.blondbattle.game.EntityCreator;
	import com.arrogames.blondbattle.game.purchases.CooldownNode;
	import org.as3commons.logging.api.getLogger;
	import org.as3commons.logging.api.ILogger;
	
	/**
	 * ...
	 * @author Robin van den Bogaard
	 */
	public class CooldownSystem extends ListIteratingSystem 
	{
		private static const logger:ILogger = getLogger(CooldownSystem);
		
		private var creator:EntityCreator;
		
		public function CooldownSystem(creator:EntityCreator) {
			super(CooldownNode, nodeUpdate, nodeAdded);
			this.creator = creator;
		}
		
		private function nodeAdded(node:CooldownNode):void {
			logger.info("Set {0} for faction {1} on cooldown for {2}s.", [node.unit.name, node.faction.name, node.cooldown.value]);
		}
		
		private function nodeUpdate(node:CooldownNode, time:Number):void {
			if (node.cooldown.isOnCooldown()) {
				node.cooldown.advance(time);
				logger.debug("{0} for faction {1} still on cooldown for {2}seconds", [node.unit.name, node.faction.name, node.cooldown.value]);
			} else {
				logger.info("Removed {0} for faction {1} from cooldown.", [node.unit.name, node.faction.name]);
				creator.destroyEntity(node.entity);
			}
		}
		
	}

}